<?php 

function arraySort($a, $b)
{
	$_sort  = 'title';
	$_order = 'asc';
	
	if (floatval($a[$_sort]) == floatval($b[$_sort])) {
		return 0; 
	}
	
	if (strtolower($_order) == 'asc') {
		return (floatval($a[$_sort]) < floatval($b[$_sort])) ? -1 : 1;
	} else {
		return (floatval($a[$_sort]) < floatval($b[$_sort])) ? 1 : -1;
	}
}

$counter = 0;
$newArr = array();

foreach ($data as $item) {
	if (array_key_exists($item['hotelRoomId'], $newArr)) {
		$newArr[$item['hotelRoomId']]['dateField' . $item['hotelDateGroupId']] = $item['price'];
	} else {
		$newArr[$item['hotelRoomId']] = array(
			'title' => $item['title'],
			'dateField' . $item['hotelDateGroupId'] => $item['price']
		);
	}
}

foreach ($newArr as &$entity) {
	foreach ($columns as $col) {
		$findKey = 'dateField' . $col->id;
		if (!array_key_exists($findKey, $entity)) {
			$item[$findKey] = '';
		}
	}
}

usort($newArr, 'arraySort');

$objsArr = array();

foreach ($newArr as $item) {
	$counter++;
	array_push($objsArr, (object)$item);
}

$colModel = array();
$colStructure = array();

$maxRowsInColHeader = 1;

if (count($columns) > 0) {
	
	$colModel[] = 'Название';
	
	$rowStructure = new stdClass();
	$rowStructure->name  = 'title';
	$rowStructure->index = 'title';
	$rowStructure->width = intval($gridConfig['nameFieldWidth']);
	$colStructure[] = $rowStructure; 
	
	foreach ($columns as $item) {
		$hotelDates = $item->getRelated('hotelDates');
		        			
		if (count($hotelDates) > 0) {
			$strCol = '';
			$curCounter = 0;
			foreach ($hotelDates as $subItem) {
				$strCol .= date('d.m.Y', strtotime($subItem->dateStart)) . ' - ' . date('d.m.Y', strtotime($subItem->dateEnd)) . '<br />';
				$curCounter++;
			}
			
			if ($curCounter > $maxRowsInColHeader) {
				$maxRowsInColHeader = $curCounter; 
			}
			
			$colModel[] = $strCol; 
			
			$rowStructure = new stdClass();
			$rowStructure->name  = "dateField{$item->id}";
			$rowStructure->index = "dateField{$item->id}";
			$rowStructure->align = "center";
			$rowStructure->width = intval($gridConfig['pricesFieldWidth']);
			$colStructure[] = $rowStructure;
		}
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title><?php echo $this->pageTitle; ?></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="robots" content="noindex,nofollow" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/public/css/site/jquery.ui/jquery-ui-1.7.2.custom.css" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/public/css/site/jqgrid.css" />
<!--[if IE 6 ]><style>img, div { behavior: url(<?php echo SiteLayout::imagePath('iepngfix.htc') ?>) }</style><![endif]-->
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/public/js/site/iepngfix_tilebg.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/public/assets/jquery/jquery-1.3.2.min.js"></script> 
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/public/js/site/library.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/public/assets/jquery/jqgrid/i18n/grid.locale-ru.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->request->baseUrl; ?>/public/assets/jquery/jqgrid/jquery.jqGrid.min.js"></script>
<style>
body {
	padding: 0px;
	margin: 0px;
	background: none;
}
.ui-jqgrid .ui-jqgrid-htable th div{
	overflow:hidden;
	position:relative;
	height:<?php echo $maxRowsInColHeader * 16 ?>px !important;
}
.ui-jqgrid .ui-jqgrid-resize{
	height:<?php echo $maxRowsInColHeader * 16 ?>px !important;
	position:relative;
	cursor:e-resize;
	display:inline;
	overflow:hidden;
}
.s-ico {
	display:none !important;
}
</style>
</head>
<body onload="frameLoad();">
<table id="list4"></table>
<script type="text/javascript">
function frameLoad() {
	<?php if (count($columns) > 0) { ?>
	var mydata = <?php echo json_encode($objsArr) ?>;

	jQuery("#list4").jqGrid({
		datatype: 'local',
		height: getHeightOfClientArea() - 100,
		colNames: <?php echo json_encode($colModel) ?>,
		colModel: <?php echo json_encode($colStructure) ?>,
		caption: "<?php echo htmlspecialchars($hotel->title) ?><?php if ($hotel->currency != '') {?>, все цены указаны в <?php echo htmlspecialchars($hotel->currency) ?><?php } ?>",
		sortname: 'title',
		sortorder: 'asc',
		hidegrid: false  
	});

	for(var i = 0; i <= mydata.length; i++) {
		jQuery("#list4").jqGrid('addRowData', i + 1, mydata[i]);
	}
	<?php } ?>
}
</script>
</body>
</html>